const axios = require("axios");
const path = require("path");
const fs = require("fs");
const filePath = "E:\\学习\\前端\\项目\\audio" //希望把文件下载到哪里


async function testGET() {
	if (!fs.existsSync(filePath)) {
		fs.mkdirSync(filepath)
	}
	/* name是生成的文件的文件名，自定义，比如，我希望产生的文件名为test.pdf,那么name='test.pdf' */
	let name = "2.flv"
	const mypath = path.resolve(filePath, name)
	const writer = fs.createWriteStream(mypath)
	let response = await axios({
		url: "https://cn-gdfs2-cc-bcache-09.bilivideo.com/upgcxcode/67/75/362837567/362837567_nb2-1-112.flv?e=ig8euxZM2rNcNbNa7zTBhwdlhbua7bUVhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1625715544&gen=playurlv2&os=bcache&oi=3698571143&trid=0000dcb2389943f3481892193094014a1b1fu&platform=pc&upsig=f2defe52efe607762b88fb87a2935536&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=6121&mid=47706697&bvc=vod&orderid=0,3&agrr=0&logo=80000000", //需要访问的资源链接
		method: "GET",
		responseType: "stream",
		headers: { "referer": "https://www.bilibili.com" },
	})
	response.data.pipe(writer)
	return new Promise((resolve, reject) => {
		writer.on("finish", resolve)
		writer.on("error", reject)
	})
}

testGET();